package com.ferret.utils;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.ferret.bean.Constants;
import com.ferret.bean.dto.APEObject;
import com.ferret.bean.dto.SubscribeObject;
import com.ferret.bean.entity.VIIDPublish;
import com.ferret.bean.entity.VIIDServer;
import com.ferret.bean.entity.VIIDSubscribe;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;

import java.text.ParseException;
import java.util.Date;
import java.util.Objects;

public class StructCodec {


    /**
     * 卡口订阅构建
     * 备注: SubscribeDetail 有时候无法订阅单个资源, 可以多加一个,标识多个资源 例: "7,"
     *
     * @param resourceUri     卡口ID, 多个ID使用,分隔
     * @param title           订阅标题
     * @param server          服务节点
     * @param subscribeDetail 订阅资源类型  7=视频卡口目录(卡口设备) 13=车辆数据(卡口过车)
     * @return 订阅对象
     */
    public static SubscribeObject inputSubscribeBuilder(String resourceUri, String title,
                                                        String subscribeDetail, VIIDServer server, String receiveAddr) {
        SubscribeObject subscribe = new SubscribeObject();
        subscribe.setSubscribeId(randomSubscriberId(server.getServerId()));
        subscribe.setTitle(title);
        subscribe.setSubscribeDetail(subscribeDetail);
        subscribe.setResourceUri(resourceUri);
        subscribe.setApplicationName("zhumadian");
        subscribe.setApplicationOrg("zhumadian");
        subscribe.setBeginTime(DateUtil.offsetDay(new Date(), -8));
        try {
            subscribe.setEndTime(DateUtil.parseDate("2026-12-31 23:59:59"));
        } catch (Exception e) {
            subscribe.setBeginTime(DateUtil.offset(new Date(), DateField.YEAR, 50));
        }
        subscribe.setReceiveAddr(receiveAddr );
        subscribe.setReportInterval(3);
        subscribe.setReason(title);
        subscribe.setOperateType(0);
        subscribe.setSubscribeStatus(0);
        //0=卡口,1=设备,4=视图库
        subscribe.setResourceClass(4);
        subscribe.setResultImageDeclare("-1");
        subscribe.setResultFeatureDeclare(1);
        return subscribe;
    }

    public static SubscribeObject castSubscribe(VIIDPublish entity) {
        if (Objects.nonNull(entity)) {
            SubscribeObject subscribe = new SubscribeObject();
            BeanUtils.copyProperties(entity, subscribe);
            return subscribe;
        }
        return null;
    }

    public static VIIDSubscribe castSubscribe(SubscribeObject subscribe) {
        VIIDSubscribe entity = new VIIDSubscribe();
        BeanUtils.copyProperties(subscribe, entity);
        return entity;
    }

    public static VIIDPublish castPublish(SubscribeObject subscribe) {
        VIIDPublish entity = new VIIDPublish();
        BeanUtils.copyProperties(subscribe, entity);
        return entity;
    }


    public static SubscribeObject castPublish(VIIDPublish publish) {
        SubscribeObject subscribe = new SubscribeObject();
        BeanUtils.copyProperties(publish, subscribe);
        return subscribe;
    }


    private static String randomSubscriberId(String organization) {
        if (StrUtil.length(organization) < 6)
            organization = "431000";
        String prefix = organization.substring(0, 6);
        String suffix = "000000";
        String op = "03";
        String timestamp = DateUtil.format(new Date(), "yyyyMMddHHmm");
        String random = RandomUtil.randomNumbers(7);
        // 6+6+2+12+7
        return prefix + suffix + op + timestamp + random;
    }

    public static void main(String[] args) {
        String mm = randomSubscriberId("41132420215032151357");
        Console.log(mm);
    }

}
